/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.proyecto;

import com.proyecto.datos.EntityCatedratico;
import com.proyecto.datos.EntityGrupo;
import com.proyecto.datos.EntityHorario;
import com.proyecto.datos.EntityHorarioMat;
import com.proyecto.datos.EntityMateria;
import com.proyecto.modelado.Catedratico;
import com.proyecto.modelado.DetalleHorarioMateria;
import com.proyecto.modelado.Grupo;
import com.proyecto.modelado.Horario;
import com.proyecto.modelado.Materia;
import java.util.ArrayList;
import javax.swing.DefaultComboBoxModel;
import javax.swing.DefaultListModel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author mario
 */
public class frmModificarGrupoClase extends javax.swing.JInternalFrame {

    Materia mate;
    Horario hora;
    Catedratico cate;
    Grupo grupo;
    DetalleHorarioMateria detahorMat;
    EntityMateria entMate;
    EntityHorario entHora;
    EntityCatedratico entCate;
    EntityGrupo entGrupo;
    ArrayList arryTable;
    int id_materia;
    int id_horario;
    int id_catedratico;
    int id_grupo;

    public void CargarcomboboxHorario() {
        ArrayList arry1 = new ArrayList();
        hora = new Horario();
        DefaultListModel List_Model = new DefaultListModel();
        JList list1 = new JList();

        arry1 = hora.MostrarCatalagoHorarios();
        List_Model.addElement("Seleccione");
        if (arry1 != null) {
            for (int i = 0; i < arry1.size(); i++) {
                entHora = (EntityHorario) arry1.get(i);
                List_Model.addElement(entHora.getHorario());
            }
        } else {
            JOptionPane.showMessageDialog(rootPane, "No se recuperaron datos de la tabla Materia", "CONFIRMACIÓN", JOptionPane.INFORMATION_MESSAGE);
        }
        list1.setModel(List_Model);
        this.jcmbHorario.setModel(new DefaultComboBoxModel(List_Model.toArray()));
        this.jcmbHorario.setSelectedIndex(0);
    }

    public void CargarComboBoxCatedratico() {
        ArrayList arry2 = new ArrayList();
        cate = new Catedratico();
        DefaultListModel List_Model2 = new DefaultListModel();
        JList list2 = new JList();
        arry2 = cate.MostrarCatedraticos();
        List_Model2.addElement("Seleccione");
        if (arry2 != null) {
            for (int i = 0; i < arry2.size(); i++) {
                entCate = (EntityCatedratico) arry2.get(i);
                List_Model2.addElement(entCate.getNombre().concat(" ").concat(entCate.getApellido()));
            }
        } else {
            JOptionPane.showMessageDialog(rootPane, "No se recuperaron datos de la tabla Materia", "CONFIRMACIÓN", JOptionPane.INFORMATION_MESSAGE);
        }
        list2.setModel(List_Model2);
        this.jcmbCatedratico.setModel(new DefaultComboBoxModel(List_Model2.toArray()));
        this.jcmbCatedratico.setSelectedIndex(0);
    }

    public void CargarComboGrupo() {
        ArrayList arry3 = new ArrayList();
        grupo = new Grupo();
        DefaultListModel List_Model3 = new DefaultListModel();
        JList list3 = new JList();
        arry3 = grupo.MostrarGrupos();
        List_Model3.addElement("Seleccione");
        if (arry3 != null) {
            for (int i = 0; i < arry3.size(); i++) {
                entGrupo = (EntityGrupo) arry3.get(i);
                List_Model3.addElement(entGrupo.getGrupo());
            }
        } else {
            JOptionPane.showMessageDialog(rootPane, "No se recuperaron datos de la tabla Materia", "CONFIRMACIÓN", JOptionPane.INFORMATION_MESSAGE);
        }
        list3.setModel(List_Model3);
        this.jcmbGrupo.setModel(new DefaultComboBoxModel(List_Model3.toArray()));
        this.jcmbGrupo.setSelectedIndex(0);
    }

    public void PopulatTabla() {

        DefaultTableModel tabla;
        Object[] fila = new Object[5];

        try {
            String[] columnName = {"ID", "Materia", "Horario", "Catedratico", "Grupo de Clase"};
            tabla = new DefaultTableModel(null, columnName);
            //populamos el arraylist de la tabla
            arryTable = new ArrayList();
            detahorMat = new DetalleHorarioMateria();
            EntityHorarioMat entHorMat = new EntityHorarioMat();
            arryTable = detahorMat.MostrarCatalagoHorariosMateria();
            if (arryTable != null) {
                for (int i = 0; i < arryTable.size(); i++) {
                    entHorMat = (EntityHorarioMat) arryTable.get(i);
                    fila[0] = entHorMat.getId_horario_materia();
                    fila[1] = entHorMat.getEntMateria();
                    fila[2] = entHorMat.getEntHorario();
                    fila[3] = entHorMat.getEntCatedratico().concat(" ").concat(entHorMat.getEntCatedraticoApellido());
                    fila[4] = entHorMat.getGrupo();
                    tabla.addRow(fila);
                }
                this.jtbleGrupos.setModel(tabla);
            } else {
                JOptionPane.showMessageDialog(rootPane, "No se recuperaron datos de la base de datos", "CONFIRMACIÓN", JOptionPane.INFORMATION_MESSAGE);
            }
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(rootPane, ex.toString());
        }
    }

    public void bloquearControl() {
        this.jtxtID.setEnabled(false);
        this.jtxtMateria.setEnabled(false);
    }

    public void habilitarControl() {
        this.jtxtID.setEnabled(true);
        this.jtxtMateria.setEnabled(true);
    }

    public frmModificarGrupoClase() {
        initComponents();

        this.setTitle("Modificar/Eliminar grupos de clase");
        PopulatTabla();
        bloquearControl();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jSeparator1 = new javax.swing.JSeparator();
        jcmbGrupo = new javax.swing.JComboBox();
        jScrollPane2 = new javax.swing.JScrollPane();
        jtbleGrupos = new javax.swing.JTable();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jcmbHorario = new javax.swing.JComboBox();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jcmbCatedratico = new javax.swing.JComboBox();
        jLabel6 = new javax.swing.JLabel();
        jtxtID = new javax.swing.JTextField();
        jbtnCargar = new javax.swing.JButton();
        jbtnActualizar = new javax.swing.JButton();
        jtbneliminar = new javax.swing.JButton();
        jtxtMateria = new javax.swing.JTextField();

        setClosable(true);
        setIconifiable(true);
        setResizable(true);

        jSeparator1.setOrientation(javax.swing.SwingConstants.VERTICAL);

        jcmbGrupo.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                jcmbGrupoItemStateChanged(evt);
            }
        });

        jtbleGrupos.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {

            }
        ));
        jScrollPane2.setViewportView(jtbleGrupos);

        jLabel1.setFont(new java.awt.Font("DejaVu Sans Condensed", 1, 18)); // NOI18N
        jLabel1.setText("Grupos de Clase");

        jLabel2.setText("Materia:");

        jLabel3.setText("Horario:");

        jcmbHorario.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                jcmbHorarioItemStateChanged(evt);
            }
        });

        jLabel4.setText("Catedratico:");

        jLabel5.setText("Grupo:");

        jcmbCatedratico.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                jcmbCatedraticoItemStateChanged(evt);
            }
        });

        jLabel6.setText("ID:");

        jtxtID.setText("ID");

        jbtnCargar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/proyecto/imagenes/cargar.png"))); // NOI18N
        jbtnCargar.setText("Cargar");
        jbtnCargar.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jbtnCargarMouseClicked(evt);
            }
        });

        jbtnActualizar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/proyecto/imagenes/editar.png"))); // NOI18N
        jbtnActualizar.setText("Actualizar");
        jbtnActualizar.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jbtnActualizarMouseClicked(evt);
            }
        });

        jtbneliminar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/proyecto/imagenes/images (2).jpg"))); // NOI18N
        jtbneliminar.setText("Eliminar");
        jtbneliminar.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jtbneliminarMouseClicked(evt);
            }
        });

        jtxtMateria.setText("Materia");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(19, 19, 19)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel4)
                            .addComponent(jLabel3)
                            .addComponent(jLabel2)
                            .addComponent(jLabel5)
                            .addComponent(jLabel6))
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(43, 43, 43)
                                .addComponent(jLabel1))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(28, 28, 28)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jtxtID, javax.swing.GroupLayout.PREFERRED_SIZE, 53, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jcmbHorario, javax.swing.GroupLayout.PREFERRED_SIZE, 242, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jcmbCatedratico, javax.swing.GroupLayout.PREFERRED_SIZE, 242, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jcmbGrupo, javax.swing.GroupLayout.PREFERRED_SIZE, 134, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jtxtMateria, javax.swing.GroupLayout.PREFERRED_SIZE, 106, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(47, 47, 47)
                        .addComponent(jbtnCargar, javax.swing.GroupLayout.PREFERRED_SIZE, 99, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jbtnActualizar, javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jtbneliminar, javax.swing.GroupLayout.PREFERRED_SIZE, 112, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(18, 18, 18)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 9, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 533, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jSeparator1)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 431, Short.MAX_VALUE)
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addGap(28, 28, 28)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(jtxtID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(19, 19, 19)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jtxtMateria, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(25, 25, 25)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jcmbHorario, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jcmbCatedratico, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4))
                .addGap(25, 25, 25)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(jcmbGrupo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(66, 66, 66)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jbtnCargar, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jbtnActualizar)
                    .addComponent(jtbneliminar, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(27, 27, 27))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jcmbGrupoItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_jcmbGrupoItemStateChanged
        ArrayList arryItem4 = new ArrayList();
        arryItem4 = grupo.MostrarGrupos();
        if (arryItem4 != null) {
            for (int i = 0; i < arryItem4.size(); i++) {
                entGrupo = (EntityGrupo) arryItem4.get(i);
                if (this.jcmbGrupo.getSelectedItem().equals(entGrupo.getGrupo())) {
                    id_grupo = entGrupo.getId_grupo();
                }
            }
        }
    }//GEN-LAST:event_jcmbGrupoItemStateChanged

    private void jcmbHorarioItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_jcmbHorarioItemStateChanged
        ArrayList arryItem2 = new ArrayList();
        arryItem2 = hora.MostrarCatalagoHorarios();
        if (arryItem2 != null) {
            for (int i = 0; i < arryItem2.size(); i++) {
                entHora = (EntityHorario) arryItem2.get(i);
                if (this.jcmbHorario.getSelectedItem().equals(entHora.getHorario())) {
                    id_horario = entHora.getId_horario();
                }
            }
        }
    }//GEN-LAST:event_jcmbHorarioItemStateChanged

    private void jcmbCatedraticoItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_jcmbCatedraticoItemStateChanged
        ArrayList arryItem3 = new ArrayList();
        arryItem3 = cate.MostrarCatedraticos();
        if (arryItem3 != null) {
            for (int i = 0; i < arryItem3.size(); i++) {
                entCate = (EntityCatedratico) arryItem3.get(i);
                if (this.jcmbCatedratico.getSelectedItem().equals(entCate.getNombre().concat(" ").concat(entCate.getApellido()))) {
                    id_catedratico = entCate.getId();
                }
            }
        }
    }//GEN-LAST:event_jcmbCatedraticoItemStateChanged

    private void jbtnCargarMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jbtnCargarMouseClicked

        int fila = this.jtbleGrupos.getSelectedRow();
        this.jtxtID.setText(String.valueOf(this.jtbleGrupos.getValueAt(fila, 0)));
        this.jtxtMateria.setText(String.valueOf(this.jtbleGrupos.getValueAt(fila, 1)));
        CargarComboBoxCatedratico();
        CargarComboGrupo();
        CargarcomboboxHorario();
    }//GEN-LAST:event_jbtnCargarMouseClicked

    private void jtbneliminarMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jtbneliminarMouseClicked

        try {
            int id_grupo_clase = Integer.parseInt(this.jtxtID.getText());

            detahorMat = new DetalleHorarioMateria();
            String msj = detahorMat.eliminarGrupoMateria(id_grupo_clase);

            JOptionPane.showMessageDialog(rootPane, msj, "Confirmación", JOptionPane.INFORMATION_MESSAGE);

            PopulatTabla();

        } catch (Exception ex) {
            JOptionPane.showMessageDialog(rootPane, ex.toString(), "Error", JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_jtbneliminarMouseClicked

    private void jbtnActualizarMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jbtnActualizarMouseClicked

        try {
            int id_detalle_clase = Integer.parseInt(this.jtxtID.getText());
            int id_horario = this.id_horario;
            int id_catedratico = this.id_catedratico;
            int id_grupo = this.id_grupo;

            detahorMat = new DetalleHorarioMateria();
            String msj = detahorMat.ModificarGrupoMateria(id_detalle_clase, id_horario, id_catedratico, id_grupo);
            JOptionPane.showMessageDialog(rootPane, msj, "Confirmación", JOptionPane.INFORMATION_MESSAGE);

            PopulatTabla();
            
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(rootPane, ex.toString(), "Error", JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_jbtnActualizarMouseClicked
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JButton jbtnActualizar;
    private javax.swing.JButton jbtnCargar;
    private javax.swing.JComboBox jcmbCatedratico;
    private javax.swing.JComboBox jcmbGrupo;
    private javax.swing.JComboBox jcmbHorario;
    private javax.swing.JTable jtbleGrupos;
    private javax.swing.JButton jtbneliminar;
    private javax.swing.JTextField jtxtID;
    private javax.swing.JTextField jtxtMateria;
    // End of variables declaration//GEN-END:variables
}
